<?php

namespace app\app\controller;

use app\app\model\ProjectDKModel;
use app\app\model\ProjectModel;
use app\app\model\ProjectPhoneModel;

use app\app\model\ProjectReportRemindModel;
use app\app\model\UserModel;
use app\BaseController;


class Task extends BaseController
{

    /**
     *  定时更新打卡时间
     */
    public function clockIn()
    {
        $start_time = date('Y-m-d 00:00:00', time());
        $end_time = date('Y-m-d  23:59:59', time());
        $where = "dk_time_in BETWEEN '" . $start_time . "' AND '" . $end_time . "' AND dk_time_out is null";
        $data['dk_time_out'] = date('Y-m-d 21:00:00', time());
        $list = ProjectDKModel::where($where)->update($data);
        $this->success('success', $list);
    }


    /**
     * 每周给项目经理添加周报待办
     */
    public function reportTask()
    {
        $date = date('w');
        if ($date == 4) {
            $timestamp = time();
            $start_time = date('Y-m-d 00:00:00', strtotime("this week Monday", $timestamp));
            $end_time = date("Y-m-d 23:59:59", strtotime("$start_time +6 day"));
            $lz_total_arr = $this->project_user($start_time, $end_time);
            $todo_task_list = array();
            foreach ($lz_total_arr as $key => $value) {
                $item['project_id'] = $value['project_id'];
                $item['project_name'] = $value['project_name'];
                $item['user_name'] = $value['user_name'];
                array_push($todo_task_list, $item);
            }
            $res = (new \app\app\model\ProjectReportRemindModel)->saveAll($todo_task_list, false);
        }

    }


    public function project_user($start_time, $end_time)
    {
        $where[] = ['tc_time', '>=', $start_time];
        $where[] = ['kg_time', '<=', $end_time];
        $where[] = ['is_parent', '=', 1];
        $where[] = ['project_period', '=', 2];
        $list = ProjectModel::field('project_name,id,link_user_id')->where($where)->select();
        $user_info = UserModel::field('user_name,nick_name')->where('del_flag', 0)->select();

        $project_user = array();
        foreach ($list as $key => $value) {
            $user_arr = explode(',', $value['link_user_id']);
            foreach ($user_arr as $k => $v) {
                if ($v) {
                    foreach ($user_info as $m => $n) {
                        if ($v == $n['user_name']) {
                            $item['user_name'] = $n['nick_name'];
                        }
                    }
                    $item['project_name'] = $value['project_name'];
                    $item['project_id'] = $value['id'];
                    array_push($project_user, $item);
                }
            }
        }
        return $project_user;

    }


}
